package com.shopping.huaju.commodity.controller;

import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shopping.huaju.commodity.entity.BrandEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.shopping.huaju.commodity.entity.CategoryBrandRelationEntity;
import com.shopping.huaju.commodity.service.CategoryBrandRelationService;
import com.shopping.huaju.common.utils.PageUtils;
import com.shopping.huaju.common.utils.R;



/**
 * 品牌分类关联表
 *
 * @author tofu
 * @email tofu@qq.com
 * @date 2023-11-07 15:20:17
 */
@RestController
@RequestMapping("commodity/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		//categoryBrandRelationService.save(categoryBrandRelation);
        // 使用自定义接口 categoryBrandRelation 将缺少的字段补充完整再添加入库
        categoryBrandRelationService.saveCategoryBrandRelationEntity(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 根据 brandId 获取该 商标对应的所有关联关系
     */
    @RequestMapping("/getByBrandId")
    public R getBrandCategoryRelationByBrandId(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelationEntity> relationsByBrand = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
        return R.ok().put("data",relationsByBrand);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/brands/list")
    public R getCatBrands(@RequestParam(value = "catId")Long catId){
        List<BrandEntity> brands = categoryBrandRelationService.getBrandsByCategoryId(catId);
        return R.ok().put("data",brands);
    }

}
